package com.xinsoft.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinsoft.common.exception.BDException;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.ScmOutsourcingOutstockDTO;
import com.xinsoft.entity.dto.ScmOutsourcingOutstockParam;
import com.xinsoft.entity.po.ScmOutsourcingOutstock;
import com.xinsoft.entity.po.ScmOutsourcingOutstockItem;
import com.xinsoft.entity.vo.ScmOutsourcingOutstockDetailVo;
import com.xinsoft.entity.vo.ScmOutsourcingOutstockPageVo;
import com.xinsoft.mapper.ScmOutsourcingOutstockItemMapper;
import com.xinsoft.service.ScmOutsourcingOutstockItemService;
import com.xinsoft.service.ScmOutsourcingOutstockService;
import com.xinsoft.mapper.ScmOutsourcingOutstockMapper;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.utils.TokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author ZYM
* @description 针对表【scm_outsourcing_outstock(委外出库)】的数据库操作Service实现
* @createDate 2023-09-01 10:38:54
*/
@Service
public class ScmOutsourcingOutstockServiceImpl extends ServiceImpl<ScmOutsourcingOutstockMapper, ScmOutsourcingOutstock>
    implements ScmOutsourcingOutstockService{

    @Resource
    private ScmOutsourcingOutstockItemMapper outsourcingOutstockItemMapper;

    @Autowired
    private ScmOutsourcingOutstockItemService outsourcingOutstockItemService;
    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    @Override
    public IPage<ScmOutsourcingOutstockPageVo> findPageListByParam(ScmOutsourcingOutstockParam param) {
        Page<ScmOutsourcingOutstockPageVo> page = new Page<>(param.getCurrentPage(), param.getPageSize());
        IPage<ScmOutsourcingOutstockPageVo> pageList = baseMapper.findPageListByParam(page, param);
        return pageList;
    }

    @Override
    public List<ScmOutsourcingOutstockPageVo> findListByParam(ScmOutsourcingOutstockParam param) {
        return baseMapper.findPageListByParam(param);
    }

    @Override
    public Integer tempSave(ScmOutsourcingOutstockDTO dto) {
        ScmOutsourcingOutstock outstock = new ScmOutsourcingOutstock();
        BeanUtils.copyProperties(dto, outstock);
        outstock.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        List<ScmOutsourcingOutstockItem> itemList = dto.getScmOutsourcingOutstockItemList();
        // 保存或新增
        if (Objects.isNull(outstock.getId())) {
            // 更新流水号
            sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.OUTSOURCING_OUTSTOCK_DOCUMENT);
        } else {
            outsourcingOutstockItemService.remove(Wrappers.lambdaQuery(ScmOutsourcingOutstockItem.class).eq(ScmOutsourcingOutstockItem::getOutstockId, outstock.getId()));
        }
        this.saveOrUpdate(outstock);
        // 保存明细
        itemList.forEach(item -> {
            item.setOutstockId(outstock.getId());
        });
        outsourcingOutstockItemService.saveBatch(itemList);
        return outstock.getId();
    }

    @Override
    public ScmOutsourcingOutstockDetailVo detail(Integer id) {
        ScmOutsourcingOutstockDetailVo vo = baseMapper.getOutstockById(id);
        vo.setScmOutsourcingOutstockItemList(outsourcingOutstockItemMapper.getItemListByOutstockId(id));
        return vo;
    }

    @Override
    public void del(List<Integer> ids) {
        List<ScmOutsourcingOutstock> list = this.list(Wrappers.lambdaQuery(ScmOutsourcingOutstock.class)
                .eq(ScmOutsourcingOutstock::getAuditStatus, Const.AUDIT_STATUS_NORMAL)
                .in(ScmOutsourcingOutstock::getId, ids));
        if (list.size() > 0) {
            throw new BDException("操作失败，已审核的数据不能删除");
        }
        outsourcingOutstockItemService.remove(Wrappers.lambdaQuery(ScmOutsourcingOutstockItem.class).in(ScmOutsourcingOutstockItem::getOutstockId, ids));
        this.removeByIds(ids);
    }

    @Override
    public void audit(Integer id) {
        ScmOutsourcingOutstock outstock = this.getById(id);
        if (Const.AUDIT_STATUS_NORMAL.equals(outstock.getAuditStatus())) {
            throw new BDException("该单据已审核，无需重复审核");
        }
        outstock.setAuditId(TokenUtil.getUserId());
        outstock.setAuditTime(new Date());
        outstock.setAuditStatus(Const.AUDIT_STATUS_NORMAL);
        this.updateById(outstock);
    }

    @Override
    public void reverseAudit(Integer id) {
        ScmOutsourcingOutstock outstock = this.getById(id);
        if (Const.AUDIT_STATUS_WAIT.equals(outstock.getAuditStatus())) {
            throw new BDException("该单据未审核，无需反审核");
        }
        // TODO 产生下游单据不能反审核
        // int citeCount = baseMapper.getOutsourcingOutstockCiteCount(id);
        // if (citeCount > 0) {
        //     throw new BDException("该单据已产生下游单据，不能反审核");
        // }
        outstock.setAuditId(null);
        outstock.setAuditTime(null);
        outstock.setAuditStatus(Const.AUDIT_STATUS_WAIT);
        this.updateById(outstock);
    }
}




